package com.timoon.sample.web.role;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.timoon.app.utils.Constantor;
import com.timoon.component.panel.jqgrid.JqGridHelper;
import com.timoon.component.panel.jqgrid.JqGridParameter;
import com.timoon.component.panel.jqgrid.JqGridService;
import com.timoon.sample.dao.ResourceDAO;
import com.timoon.sample.dao.RoleDAO;
import com.timoon.sample.dao.RoleResourceDAO;
import com.timoon.sample.dao.model.Role;
import com.timoon.sample.dao.model.RoleExample;
import com.timoon.sample.dao.model.RoleResource;
import com.timoon.sample.dao.model.RoleResourceExample;

@Service(value="com.flt.sample.role.RoleJqGridServiceImpl")
public class RoleJqGridServiceImpl implements JqGridService {

	@Resource 
	private RoleDAO roleDAOImpl;
	
	@Resource
	private ResourceDAO resourceDAOImpl;
	
	@Resource
	private RoleResourceDAO roleResourceDAOImpl;
	
	@Override
	public String getGridData(HttpServletRequest request) {
		// TODO Auto-generated method stub
		
		JqGridParameter param=new JqGridParameter();
		JqGridHelper helper=new JqGridHelper(){
			@Override
			protected Map<String, Object> coverRecord(Map<String, Object> record) {
				// TODO Auto-generated method stub
				if(record.containsKey("status")){
					Integer status=Integer.valueOf(String.valueOf(record.get("status")));
					String statusText=Constantor.getStatusFromStatusMapByKey(status);
					record.put("statusText", statusText);
				}
				
				return record;
			}
		};
		
		Map<String,String> paramMap=helper.coverParameterMap(request);
		param.setMap(paramMap);
		
		Integer records=roleDAOImpl.countByExample(new RoleExample());
		Integer rows=Integer.valueOf(paramMap.get("rows").toString());
		Integer page=Integer.valueOf(paramMap.get("page").toString());
		Integer startIndex=helper.getStartIndex(rows,page,records);
		
		param.setPage(page);
		param.setRows(rows);
		param.setStartIndex(startIndex);
		
		List<Map<String,Object>> data=this.roleDAOImpl.pageRole(param);
		
		//将status转化为中文
		return helper.getJqJSON(data);
	}

	@Override
	public Integer countGridData(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void deleteByIds(String deleteIds) {
		// TODO Auto-generated method stub
		if(deleteIds!=null){
			String[] ids=deleteIds.split(",");
			for(String id:ids){
				this.roleDAOImpl.deleteByPrimaryKey(Integer.valueOf(id));
			}
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public void saveOrUpdateRecord(Map<String, Object> map) {
		// TODO Auto-generated method stub
		final Role role=(Role) map.get("role");
		if(role.getId()==null){
			this.roleDAOImpl.insert(role);
			
			//取出刚存的Role
			List<Role> roles=this.roleDAOImpl.selectByExample(new RoleExample(){
				{
					RoleExample.Criteria ct=this.createCriteria();
					ct.andTextEqualTo(role.getText());
					ct.andRemarkEqualTo(role.getRemark());
				}
			});
			
			Role justRole=roles.get(0);
			
			Set<String> resourceIds=(Set<String>) map.get("resourceIds");
			if(resourceIds!=null){
				//创建role-resource记录
				for(String id:resourceIds){
					RoleResource roleResource=new RoleResource();
					roleResource.setResourceId(Integer.valueOf(id));
					roleResource.setRoleId(justRole.getId());
					this.roleResourceDAOImpl.insert(roleResource);
				}
			}
		}else{
			this.roleDAOImpl.updateByPrimaryKey(role);
			
			//先删除之前的当前role的所有role-resource,再根据传过来的值创建新的
			this.roleResourceDAOImpl.deleteByExample(new RoleResourceExample(){
				{
					this.createCriteria().andRoleIdEqualTo(role.getId());
				}
			});
			
			String[] resourceIds=(String[]) map.get("resourceIds");
			if(resourceIds!=null){
				//创建role-resource记录
				for(String id:resourceIds){
					RoleResource roleResource=new RoleResource();
					roleResource.setResourceId(Integer.valueOf(id));
					roleResource.setRoleId(role.getId());
					this.roleResourceDAOImpl.insert(roleResource);
				}
			}
			
		}
	}

	@Override
	public Role loadRecordById(String id) {
		// TODO Auto-generated method stub
		return this.roleDAOImpl.selectByPrimaryKey(Integer.valueOf(id));
	}

	public RoleDAO getRoleDAOImpl() {
		return roleDAOImpl;
	}

	public void setRoleDAOImpl(RoleDAO roleDAOImpl) {
		this.roleDAOImpl = roleDAOImpl;
	}

	public ResourceDAO getResourceDAOImpl() {
		return resourceDAOImpl;
	}

	public void setResourceDAOImpl(ResourceDAO resourceDAOImpl) {
		this.resourceDAOImpl = resourceDAOImpl;
	}

	public RoleResourceDAO getRoleResourceDAOImpl() {
		return roleResourceDAOImpl;
	}

	public void setRoleResourceDAOImpl(RoleResourceDAO roleResourceDAOImpl) {
		this.roleResourceDAOImpl = roleResourceDAOImpl;
	}

}
